


#include <iostream>
#include <ctime>

#include <boost/tr1/memory.hpp>
#include <boost/thread/thread.hpp>


#include "../classes_net/DownloadParser.h"
#include "../classes_net/URL.h"
#include "../classes_dados/ManagerBerthier.h"
#include "../classes_dados/Repository.h"
#include "../classes_outros/Config.h"
#include "../classes_outros/RobotsDownloadInterface.h"

#include "ThreadFunctionDatabase.h"

int ThreadFunctionDatabase::numPaginas = 0;




boost::mutex ThreadFunctionDatabase::numPaginasMutex;

time_t ThreadFunctionDatabase::syncLifeThread;

void * ThreadFunctionDatabase::manageDownloaderQueues(void * t) {
	std::tr1::shared_ptr<ManagerBerthier> mb = ManagerBerthier::getInstance();
	bool conseguiu;
	boost::xtime dorme;
	
	time_t miSugao;
	uint myLifeTime = Config::getDownloaderThreadlifeTime();
	
	while (true)
	{
		time(&miSugao);
		if(miSugao >= ThreadFunctionDatabase::getSyncLifeThread() + myLifeTime)
			break;
		conseguiu = mb->refillQueuesOnce();
		if (!conseguiu){
			cout << "ZzzZzzZzz" << endl;
			boost::xtime_get(&dorme, boost::TIME_UTC);
			dorme.sec += Config::getQueueThreadSleepTime();
			boost::thread::sleep(dorme);
		}
	}
	
	pthread_exit(NULL);
}

void * ThreadFunctionDatabase::getDomainRobots(void *domParam) {
	while (true) {
		Dominio * dominio = RobotsDownloadInterface::get();
		DownloadParser downloader;
		downloader.setURL(dominio->getName());
		Robots * robots =  new Robots( downloader.getRobot() );
		dominio->setRobots(robots);
	}
}


void * ThreadFunctionDatabase::downloadInit(void * t){
	int id = int(t);
	time_t miSugao;
	bool terminateThread = false;
	uint myLifeTime = Config::getDownloaderThreadlifeTime();
	uint sleepThreadTime = Config::getDownloaderThreadSleepTime();
	
	try{
	uint dormeTimeOut = 0;
		
	std::tr1::shared_ptr<URLFileDealer> urlFileDealer = URLFileDealer::getInstance();
	std::tr1::shared_ptr<ManagerBerthier> mb = ManagerBerthier::getInstance();
	DownloadParser downloadTeste;
	
	URL * tempUrl;
	list<URL> urlList;
	list<URL>::iterator it;
	
	tempUrl = mb->getNewURL(id);
	
	while(dormeTimeOut < Config::getDownloaderThreadNumSleeps()){
		
		while( tempUrl != NULL ){
			dormeTimeOut = 0;	
			
			downloadTeste.setURL(tempUrl->toString());
			urlList = downloadTeste.getNewURLs();
			
			{
				boost::mutex::scoped_lock numPaginasLock(numPaginasMutex);
				 numPaginas = Repository::getContador();
				if ((numPaginas % 300)== 0){
					cout << "numPaginas: " << numPaginas << endl;
				}
				if ((numPaginas % 200)== 0){
					cout << "Site: " << tempUrl->toString() << endl;
				}
			}
			urlFileDealer->addURLs(urlList);
			
			delete tempUrl;
			
			time(&miSugao);
			if(miSugao >= ThreadFunctionDatabase::getSyncLifeThread() + myLifeTime){
				terminateThread = true; 
				break;
			}
				
			tempUrl = mb->getNewURL(id);
		}
		
		if(terminateThread) break;
	
		// dorme 		
		boost::xtime dorme;
		boost::xtime_get(&dorme, boost::TIME_UTC);
		dorme.sec += sleepThreadTime;
		boost::thread::sleep(dorme);
		
		dormeTimeOut ++;
		
		time(&miSugao);
		if(miSugao >= ThreadFunctionDatabase::getSyncLifeThread() + myLifeTime)
			break;
		
		tempUrl = mb->getNewURL(id);
	}
	
	}catch(...){
		cout << "MC causou problema em algo...Ver ThreadFunctionDatabase" << endl;
	}
	
	pthread_exit(NULL);
}



